package com.upu173.music.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.andbase.library.asynctask.AbTask;
import com.andbase.library.asynctask.AbTaskItem;
import com.andbase.library.asynctask.AbTaskObjectListener;
import com.andbase.library.utils.AbDateUtil;
import com.andbase.library.utils.AbDialogUtil;
import com.andbase.library.utils.AbToastUtil;
import com.upu173.music.R;
import com.upu173.music.fragment.SongListFragment;
import com.upu173.music.global.PlayerData;
import com.upu173.music.model.PlayList;
import com.upu173.music.model.Song;
import com.upu173.music.model.SongFolder;
import com.upu173.music.utils.MusicUtil;

import java.util.ArrayList;
import java.util.List;


public class MusicListActivity extends MusicBaseActivity {

    private SongListFragment songListFragment;
    private String folderName  = null;
    private String folderPath  = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_activity_list);

        Toolbar toolbar = (Toolbar)this.findViewById(R.id.toolbar);

        folderName = getIntent().getStringExtra("folderName");
        folderPath = getIntent().getStringExtra("folderPath");

        initToolbar(toolbar,folderName,true);

        songListFragment = new SongListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("folderPath",folderPath);
        songListFragment.setArguments(bundle);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.frame_layout, songListFragment).commit();
    }

    public void onSongClick(int position){
        //添加到播放列表
        Song song = songListFragment.items.get(position);
        PlayList playList = new PlayList();
        playList.setName(song.getName());
        playList.setPath(song.getPath());
        playList.setParentPath(song.getParentPath());
        playList.setSinger(song.getSinger());
        playList.setSize(song.getSize());
        playList.setDuration(song.getDuration());
        playList.setUpdateTime(AbDateUtil.getCurrentDate(AbDateUtil.dateFormatYMDHMS));

        musicBarFragment.playSong(playList);

    }

    public void addSongClick(final int position){
        AbDialogUtil.showLoadingDialog(MusicListActivity.this,-1,"添加到播放列表...");
        AbTask task = AbTask.newInstance();
        AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener<List<Song>>() {
            @Override
            public List<Song> getObject() {
                Song song = songListFragment.items.get(position);
                List<Song> songList = new ArrayList<>();
                songList.add(song);
                MusicUtil.addPlayList(MusicListActivity.this,songList);
                return songList;
            }

            @Override
            public void update(List<Song> songList) {
                AbDialogUtil.removeDialog(MusicListActivity.this);
                AbToastUtil.showToast(MusicListActivity.this,"添加到播放列表：" + songList.size() + "首");
            }
        });
        task.execute(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
